/* 
 * File:   CodecTest.cpp
 * Author: max
 *
 * Created on Apr 28, 2013, 2:16:23 PM
 */

#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <stdint.h>
#include <fstream>
#include <string.h>

#include "common_Codec.h"

using namespace std;
using namespace TP;
/*
 * Simple C++ Test Suite
 */

void test1() {
    std::cout << "CodecTest test 1" << std::endl;
    
    uint8_t buffer[4] = {0x00, 0xAF, 0xFF, 0xA9};

    std::stringstream ss;
    ss << std::hex << std::setfill('0');
    for (int i = 0; i < 4; ++i)
    {
        ss << std::setw(2) << static_cast<unsigned>(buffer[i]);
    }    
    
    string representacion;
    ss >> representacion;
    
    cout << endl << representacion << endl;
    
}

void test2() {
    std::cout << "CodecTest test 2" << std::endl;
    
    Codec codec;
    
    uint8_t buffer[4] = {0x00, 0xAF, 0xFF, 0xA9};
    string str;
    codec.codificar(str, buffer, 4);
    
    cout << endl << str << endl;
    
    uint8_t* buffer2;
    uint32_t longitud;
    codec.decodificar(buffer2, longitud, str);
    
    string str2;
    codec.codificar(str2, buffer2, longitud);
    
    cout << endl << str << endl;
    
    delete buffer2;
    
    if (str != str2)
        std::cout << "%TEST_FAILED% time=0 testname=test2 (CodecTest) message=decode(code(A)) != A" << std::endl;
}

void test3(){
    
    std::cout << "CodecTest test 3" << std::endl;
    try
    {
        //ifstream input ("/home/Max/facultad/75.42.TPS/75.42.TP4.2013.1/Fuentes/75.42.TP4.2013.1/Comun/tests/basic", ios::in|ios::binary);
        
        ifstream input ("/home/Max/facultad/75.42.TPS/75.42.TP4.2013.1/Fuentes/75.42.TP4.2013.1/GrupoTaller/basic", ios::in|ios::binary);
        if (!input.is_open()) throw 1;

        // Codifico el archivo.
        
        uint8_t* buffer = new uint8_t [256];
        
        uint32_t fileSize = 0
               , bytesLeidos = 0;
        
        Codec codec;
        string content=""
             , contentAux = "";
        
        while (!input.eof())
        {
            bzero (buffer, 256);
            input.read(reinterpret_cast <char*> (buffer), 256);
            content += codec.codificar(contentAux, buffer, input.gcount());
        }
        
        input.close();
        
        // Guardo el archivo codificado en un temporal.
        
        //ofstream output ("/home/Max/facultad/75.42.TPS/75.42.TP4.2013.1/Fuentes/75.42.TP4.2013.1/Comun/tests/basic.tmp");
        
        ofstream output ("/home/Max/facultad/75.42.TPS/75.42.TP4.2013.1/Fuentes/75.42.TP4.2013.1/GrupoTaller/basic.tmp");
        if (!output.is_open()) throw 2;
        
        output.write(content.c_str(), content.length());
        
        output.close();
        
    }
    catch (int ex)
    {
        string msg = "";
        
        if (1 == ex)
            msg = "Fallo al abrir el archivo de entrada.";

        if (2 == ex)
            msg = "Fallo al abrir el archivo de salida.";
                    
        std::cout << "%TEST_FAILED% time=0 testname=test3 (CodecTest) message=" << msg << std::endl;
    }
}

void test4(){
    std::cout << "CodecTest test 4" << std::endl;
    try
    {
        // Abro para lectura el archivo codificado.
        //ifstream input ("/home/Max/facultad/75.42.TPS/75.42.TP4.2013.1/Fuentes/75.42.TP4.2013.1/Comun/tests/basic.tmp");
        ifstream input ("/home/Max/facultad/75.42.TPS/75.42.TP4.2013.1/Fuentes/75.42.TP4.2013.1/GrupoTaller/basic.tmp");
        if (!input.is_open()) throw 1;

        string data;
        input >> data;
        
        input.close();
        
        Codec codec;
        uint8_t* buffer;
        uint32_t longitud;
        
        codec.decodificar(buffer, longitud, data);
                
        // Guardo el archivo codificado en un temporal.
        
        //ofstream output ("/home/Max/facultad/75.42.TPS/75.42.TP4.2013.1/Fuentes/75.42.TP4.2013.1/Comun/tests/basic2", ios::out|ios::binary);
        ofstream output ("/home/Max/facultad/75.42.TPS/75.42.TP4.2013.1/Fuentes/75.42.TP4.2013.1/GrupoTaller/basic2", ios::out|ios::binary);
        if (!output.is_open()) throw 2;

        output.write(reinterpret_cast<char*> (buffer), longitud);
        
        output.close();
    }
    catch (int ex)
    {
        string msg = "";
        
        if (1 == ex)
            msg = "Fallo al abrir el archivo de entrada.";

        if (2 == ex)
            msg = "Fallo al abrir el archivo de salida.";
                    
        std::cout << "%TEST_FAILED% time=0 testname=test4 (CodecTest) message=" << msg << std::endl;
    }
        

}

int main(int argc, char** argv) {
    std::cout << "%SUITE_STARTING% CodecTest" << std::endl;
    std::cout << "%SUITE_STARTED%" << std::endl;
/*
    std::cout << "%TEST_STARTED% test1 (CodecTest)" << std::endl;
    test1();
    std::cout << "%TEST_FINISHED% time=0 test1 (CodecTest)" << std::endl;

    std::cout << "%TEST_STARTED% test2 (CodecTest)\n" << std::endl;
    test2();
    std::cout << "%TEST_FINISHED% time=0 test2 (CodecTest)" << std::endl;
*/
    std::cout << "%TEST_STARTED% test3 (CodecTest)\n" << std::endl;
    test3();
    std::cout << "%TEST_FINISHED% time=0 test3 (CodecTest)" << std::endl;

    std::cout << "%TEST_STARTED% test4 (CodecTest)\n" << std::endl;
    test4();
    std::cout << "%TEST_FINISHED% time=0 test4 (CodecTest)" << std::endl;

    std::cout << "%SUITE_FINISHED% time=0" << std::endl;

    return (EXIT_SUCCESS);
}

